/*
 *
 *  * Copyright (c) 2015. For Intelligent Group.
 *
 */

package com.intelligent.ispc.export.grr;

import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.foundation.poi.ExCell;
import com.intelligent.ispc.foundation.poi.ExSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import com.intelligent.ispc.foundation.poi.SampleExcelBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

/**
 * draw GRR excel
 * Created by Ailce on 2016/9/23.
 */
public class GrrExcelBuilder extends SampleExcelBuilder {
    private static Logger logger = LoggerFactory.getLogger(GrrExcelBuilder.class);

    private Integer operator;
    private Integer trial;

    /**
     * Constructor
     * @param operator   operator
     * @param trial      trail
     */
    public GrrExcelBuilder(Integer operator, Integer trial) {
        this.operator = operator;
        this.trial = trial;
    }

    /**
     * draw excel of summary and detail
     *
     * @param path      file path of exporting excel
     * @param factory   class of filling data to cell
     */
    public void drawExcel(String path, GrrExportWorker factory) {
        Iterator<ExSheet> sheet = factory.getSheets().iterator();
        SXSSFWorkbook currentWb = factory.getCurrentWorkbook();
        OutputStream out = null;
        try {
            int i = 0;
            out = new FileOutputStream(path);
            for (; sheet.hasNext(); i++) {
                drawGrrSheet(currentWb, sheet.next(), i);
            }
            currentWb.write(out);
        } catch (Exception e) {
            logger.error("can not find directory:" + e);
            throw new ApplicationException(e.getMessage());
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private void drawGrrSheet(SXSSFWorkbook workbook, ExSheet exSheet, int i) {
        //draw excel of summary table
        if (i == 0) {
            String name = exSheet.getName();
            logger.debug("sheet name:{}", name);
            List<ExCell> exCells = exSheet.getExCells();
            SXSSFSheet sheet = workbook.createSheet(name);
            CellRangeAddress cellRangeAddressNO = new CellRangeAddress(0, 1, 0, 0);
            sheet.addMergedRegion(cellRangeAddressNO);

            CellRangeAddress cellRangeAddressItem = new CellRangeAddress(0, 1, 1, 1);
            sheet.addMergedRegion(cellRangeAddressItem);

            CellRangeAddress cellRangeAddressSpec = new CellRangeAddress(0, 0, 2, 4);
            sheet.addMergedRegion(cellRangeAddressSpec);

            CellRangeAddress cellRangeAddressRResult = new CellRangeAddress(0, 0, 5, 7);
            sheet.addMergedRegion(cellRangeAddressRResult);

            CellRangeAddress cellRangeAddressResult = new CellRangeAddress(0, 1, 8, 8);
            sheet.addMergedRegion(cellRangeAddressResult);

            final int columnWidth20 = 20;
            final int columnWidth5 = 5;
            final int columnWidth40 = 40;
            sheet.setDefaultColumnWidth(columnWidth20);
            sheet.setColumnWidth(0, columnWidth5 * 256);
            sheet.setColumnWidth(1, columnWidth40 * 256);
            drawSheet(exCells, sheet, workbook);
            try {
                sheet.flushRows();
            } catch (IOException e) {
                e.printStackTrace();
            }
            sheet = null;
        } else {
            //draw excel of detail table
            drawGRRItemsSheet(workbook, exSheet);
        }
    }
    private void drawGRRItemsSheet(SXSSFWorkbook workbook, ExSheet exSheet) {
        String name = exSheet.getName();
        logger.debug("sheet name:{}", name);
        List<ExCell> exCells = exSheet.getExCells();
        SXSSFSheet sheet = workbook.createSheet(name);

        // Merge cells of table head
        int currentRow = 1;
        final int columnNum1 = 1;
        final int columnNum7 = 7;

        CellRangeAddress testItem = new CellRangeAddress(currentRow, currentRow, columnNum1, columnNum1 + 1);
        CellRangeAddress blank = new CellRangeAddress(currentRow, currentRow, columnNum7, columnNum7 + 1);
        sheet.addMergedRegion(blank);
        sheet.addMergedRegion(testItem);
        currentRow++;
        CellRangeAddress testItemValue = new CellRangeAddress(currentRow, currentRow + 1, columnNum1, columnNum1 + 1);
        sheet.addMergedRegion(testItemValue);

        //Merge cells of normal
        currentRow = operator * trial + operator * 2 + 6;
        CellRangeAddress partRange = new CellRangeAddress(currentRow, currentRow, columnNum1, columnNum1 + 1);
        sheet.addMergedRegion(partRange);
        currentRow++;
        CellRangeAddress partAVG = new CellRangeAddress(currentRow, currentRow, columnNum1, columnNum1 + 1);
        sheet.addMergedRegion(partAVG);

       // Merge cells of anova
        currentRow = currentRow + 2;
        for (int i = 0; i < 6; i++) {
            int n = 1;
            CellRangeAddress anova = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(anova);
            currentRow++;
        }

        // Merge cells of source
        currentRow++;
        CellRangeAddress sigmaTitle = new CellRangeAddress(currentRow, currentRow, columnNum1, columnNum7);
        sheet.addMergedRegion(sigmaTitle);

        currentRow++;
        for (int i = 0; i < 8; i++) {
           int n = 1;
            CellRangeAddress source = new CellRangeAddress(currentRow, currentRow, n++, n++);
            sheet.addMergedRegion(source);
            currentRow++;
        }

        //Merge cells of last table
        currentRow++;
        final int columnNum4 = 4;
        CellRangeAddress categoryName = new CellRangeAddress(currentRow, currentRow, columnNum1, columnNum4);
        sheet.addMergedRegion(categoryName);

        final int columnNum5 = 5;
        CellRangeAddress categoryValue = new CellRangeAddress(currentRow, currentRow, columnNum5, columnNum5 + 1);
        sheet.addMergedRegion(categoryValue);

        final int columnWidth = 20;
        final int columnWidth5 = 5;
        final int columnWidth15 = 15;
        sheet.setDefaultColumnWidth(columnWidth);
        sheet.setColumnWidth(0, columnWidth5 * 256);
        sheet.setColumnWidth(1, columnWidth15 * 256);
        sheet.setColumnWidth(2, columnWidth5 * 256);
        drawSheet(exCells, sheet, workbook);
        try {
            sheet.flushRows();
        } catch (IOException e) {
            e.printStackTrace();
        }
        sheet = null;
    }
}


